﻿using System;

namespace DBMock.Builders
{
    /// <summary>
    /// 日期builder
    /// </summary>
    public class DateBuilder : Builder
    {
        private DateTime min; //最小值
        private int maxSpan; //最大间隔

        /// <summary>
        /// 初始化DateBuilder
        /// </summary>
        /// <param name="seed">随机数种子</param>
        /// <param name="minDate">最小日期</param>
        /// <param name="maxDate">最大日期</param>
        public DateBuilder(int seed, DateTime minDate, DateTime maxDate)
            : base(seed)
        {
            if (minDate < new DateTime(1753, 1, 1))
            { throw new Exception("Minimum cannot be smaller than 1753-1-1."); }

            if (maxDate > new DateTime(9999, 12, 31))
            { throw new Exception("Maximum cannot be larger than 9999-12-31."); }

            if (minDate > maxDate)
            { throw new Exception("Minimum is larger than maximum."); }

            var ts = new TimeSpan(maxDate.Ticks - minDate.Ticks);

            min = minDate.Date;
            maxSpan = (int)ts.TotalDays;
        }

        public override object Build()
        {
            int span = random.Next(maxSpan);
            return min.AddDays(span);
        }
    }
}
